package com.kingmed.kmss.framework.cpwx.service.impl;

import com.kingmed.kmss.framework.cpwx.config.WxCpConfiguration;
import com.kingmed.kmss.framework.cpwx.service.CpWxAgentMessageService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import org.springframework.stereotype.Service;

/**
 * @author liangzhi
 * @date 2024/1/22
 */
@Slf4j
@Service
public class CpWxAgentMessageServiceImpl implements CpWxAgentMessageService {


    @Override
    public void sendTextCardMessage(WxCpMessage wxCpMessage) {
        WxCpService cpService = WxCpConfiguration.getCpService(wxCpMessage.getAgentId());
        try {
            WxCpMessageSendResult result = cpService.getMessageService().send(wxCpMessage);
            if (result.getErrCode() == 0) {
                log.info("发送消息结果：{}", result);
            } else {
                log.error("发送消息结果：{}", result);
            }
        } catch (WxErrorException e) {
            throw new RuntimeException("发送企微消息失败!", e);
        }
    }
}
